<html>
<head><meta charset="utf-8"><title>serde adjacent-like tag but inside other object · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/serde.20adjacent-like.20tag.20but.20inside.20other.20object.html">serde adjacent-like tag but inside other object</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239499642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/serde%20adjacent-like%20tag%20but%20inside%20other%20object/near/239499642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio Sánchez <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/serde.20adjacent-like.20tag.20but.20inside.20other.20object.html#239499642">(May 19 2021 at 21:37)</a>:</h4>
<p>Hi all,<br>
I'm trying to deserialize with <code>serde</code> a JSON file of the form:</p>
<div class="codehilite" data-code-language="JSON"><pre><span></span><code><span class="p">{</span>
    <span class="nt">"header"</span><span class="p">:</span> <span class="p">{</span>
        <span class="nt">"tag"</span><span class="p">:</span> <span class="s2">"..."</span>
    <span class="p">},</span>
    <span class="nt">"content"</span><span class="p">:</span> <span class="p">{</span>
        <span class="err">...</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>where the fields of <code>content</code> vary depending on <code>tag</code>.</p>
<p>If the <code>tag</code> field was on the top level, this would be trivial to implement using an adjacently tagged enum like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[serde(tag = </span><span class="s">"tag"</span><span class="cp">, content = </span><span class="s">"content"</span><span class="cp">)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">Content</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>but I don't know how to indicate that <strong>the <code>tag</code> is inside <code>header</code></strong>.</p>
<p>Any suggestions?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>